#include "InputGP2X.h"
#include "ComponentManager.h"

#include <iostream>
using namespace std;

CInputGP2X::CInputGP2X(void)
{
	int nbJoysticks = SDL_NumJoysticks();
	if(nbJoysticks == 0)
		CM.LOGCONSOLE().Log()<<"no joystick \n";

	m_joystick = SDL_JoystickOpen(0);
}

CInputGP2X::~CInputGP2X(void){}

bool CInputGP2X::initialiseActionMapping(void)
{
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_UP));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_DOWN));
	actionMap.insert(pair<int, int>(eMoveLeft, GP2X_BUTTON_LEFT));
	actionMap.insert(pair<int, int>(eMoveRight, GP2X_BUTTON_RIGHT));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_UPLEFT));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_UPRIGHT));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_DOWNLEFT));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_DOWNRIGHT));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_CLICK));
	actionMap.insert(pair<int, int>(eJump, GP2X_BUTTON_A));
	actionMap.insert(pair<int, int>(eAttack, GP2X_BUTTON_B));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_X));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_Y));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_L));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_R));
	actionMap.insert(pair<int, int>(ePause, GP2X_BUTTON_START));
	actionMap.insert(pair<int, int>(eNoAction, GP2X_BUTTON_SELECT));
	actionMap.insert(pair<int, int>(eVolumeUp, GP2X_BUTTON_VOLUP));
	actionMap.insert(pair<int, int>(eVolumeDown, GP2X_BUTTON_VOLDOWN));

	return true;
}

void CInputGP2X::doUpdate(void)
{	
	//update the joystick
	SDL_JoystickUpdate();

	if(SDL_JoystickGetButton(m_joystick, (int)(*actionMap.find(eMoveLeft)).second))
	{
		CM.WORLD().addAction(eMoveLeft);
		cout<<"joystick left"<<endl;
	}

	if(SDL_JoystickGetButton(m_joystick, (int)(*actionMap.find(eMoveRight)).second))
	{
		CM.WORLD().addAction(eMoveRight);
		cout<<"joystick right"<<endl;
	}

	if(SDL_JoystickGetButton(m_joystick, (int)(*actionMap.find(eJump)).second))
	{
		CM.WORLD().addAction(eJump);
		cout<<"joystick up"<<endl;
	}

	if(SDL_JoystickGetButton(m_joystick, (int)(*actionMap.find(ePause)).second))
	{
		CM.WORLD().addAction(ePause);
		cout<<"start pressed"<<endl;
	}

	if(SDL_JoystickGetButton(m_joystick, (int)(*actionMap.find(eAttack)).second))
	{
		CM.WORLD().addAction(eAttack);
		cout<<"b button pressed"<<endl;
	}

	if(SDL_JoystickGetButton(m_joystick, (int)(*actionMap.find(eVolumeUp)).second))
	{
		CM.AUDIO().setVolume(CM.AUDIO().getVolume()+5);
		cout<<"volume up key pressed"<<endl;
	}

	if(SDL_JoystickGetButton(m_joystick, (int)(*actionMap.find(eVolumeDown)).second))
	{
		CM.AUDIO().setVolume(CM.AUDIO().getVolume()-5);
		cout<<"volume down pressed"<<endl;
	}

	if(SDL_JoystickGetButton(m_joystick, GP2X_BUTTON_L) && SDL_JoystickGetButton(m_joystick, GP2X_BUTTON_R) && 
		SDL_JoystickGetButton(m_joystick, GP2X_BUTTON_START))
	{
		CM.WORLD().addAction(eQuit);
		CM.LOGCONSOLE().Log()<<"LOG : INPUT : EXIT\n";
	}
} 
